﻿Imports System.Runtime.CompilerServices
Imports Microsoft.SharePoint
Imports System.Xml

Namespace Extensions
    Public Module SPListItemExensions

        <Extension()> _
        Public Function TryGetValue(Of T)(ByVal item As SPListItem, ByVal fieldName As String) As T
            Try
                Dim rawValue = item(fieldName)
                If rawValue IsNot Nothing Then
                    If TypeOf rawValue Is Guid Then
                        Dim obj As Object = rawValue.ToString
                        Return CType(obj, T)
                    ElseIf TypeOf rawValue Is SPFieldLookupValue Then
                        Dim returnValue As Object = rawValue.ToString
                        Return CType(returnValue, T)
                    Else
                        Return CType(rawValue, T)
                    End If
                    'Dim obj As Object = item(fieldName)
                    'If obj IsNot Nothing Then
                    '    Return CType(obj, T)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        <Extension()> _
        Public Function TryGetValue(Of T)(ByVal item As SPListItem, ByVal fieldId As Guid) As T
            Try
                Dim rawValue = item(fieldId)
                If rawValue IsNot Nothing Then
                    Return CType(rawValue, T)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
        <Extension()> _
        Public Function TryGetValue(Of T)(ByVal item As SPListItem, ByVal fieldIndex As Integer) As T
            Try
                Dim rawValue = item(fieldIndex)
                If rawValue IsNot Nothing Then
                    Return CType(rawValue, T)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Return Nothing
            End Try
        End Function

        <Extension()> _
        Public Function GetMajorVersionFile(ByVal item As SPListItem) As SPFile
            If item.ParentList.EnableVersioning Then
                For index As Integer = 0 To item.Versions.Count - 1
                    If (item.Versions(index).IsCurrentVersion OrElse item.Versions(index).Level = SPFileLevel.Published) AndAlso CInt(item.Versions(index).VersionLabel) = CDbl(item.Versions(index).VersionLabel) Then
                        If index = 0 Then
                            Return item.File
                        Else
                            Return item.File.Versions.GetVersionFromLabel(item.Versions(index).VersionLabel).File
                        End If
                    End If
                Next
            Else
                Return item.File
            End If
            Return Nothing
        End Function

        <Extension()> _
        Public Function FullUrl(ByVal item As SPListItem) As String
            Dim xmlDoc As New XmlDocument
            xmlDoc.LoadXml(item.Xml)
            Dim nsm As New XmlNamespaceManager(xmlDoc.NameTable)
            nsm.AddNamespace("z", "#RowsetSchema")
            Return xmlDoc.SelectSingleNode("z:row", nsm).Attributes("ows_EncodedAbsUrl").Value
        End Function
    End Module
End Namespace